#include "MQTTMessage.h"

MQTTMessage::MQTTMessage(const mosquitto_message *msg)
    : m_msg(new Message(msg))
{
}

uint32_t MQTTMessage::size() const
{
	return m_msg->size();
}

void *MQTTMessage::data() const
{
	return m_msg->data();
}

char *MQTTMessage::topic() const
{
	return m_msg->topic();
}

int MQTTMessage::qos() const
{
	return m_msg->qos();
}

MQTTMessage::~MQTTMessage()
{
}

MQTTMessage::Message::Message(const mosquitto_message *msg)
{
	message       = *msg;
	message.topic = (char *)malloc(strlen(msg->topic) + 1);
	strcpy(message.topic, msg->topic);

	message.payload = malloc(msg->payloadlen);
	memcpy(message.payload, msg->payload, msg->payloadlen);
}

MQTTMessage::Message::~Message()
{
	free(message.topic);
	free(message.payload);
}

uint32_t MQTTMessage::Message::size() const
{
	return message.payloadlen;
}

void *MQTTMessage::Message::data() const
{
	return message.payload;
}

char *MQTTMessage::Message::topic() const
{
	return message.topic;
}

int MQTTMessage::Message::qos() const
{
	return message.qos;
}
